﻿/**
 * 
 * @file FITKVAOneXMLWriter.h
 * @brief  VAOne XML文件写入类
 * @author LiBaojun (libaojunqd@foxmail.com)
 * @date 2024-10-11
 * 
 */
#ifndef _FITKVAONE_XML_WRITER_H___
#define _FITKVAONE_XML_WRITER_H___

#include "FITK_Interface/FITKInterfaceIO/FITKAbstractRapidXMLWriter.h"
#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"

namespace rapidxml
{
    template<class Ch>
    class xml_node;

    template<class Ch>
    class xml_document;
}

namespace Acoustics
{
    class FITKAcousticsDiscModel;
    class FITKAcousticsPhysics;
    class FITKAcousticsSEASubSysManager;
    class FITKSEASubSysSemiInfiniteFluid;
    class FITKAcousticsAbstractLoad;
    class FITKSEAFaceManager;
    class FITKJunctionManager;
    class FITKAbstractSEASubSys;
    class FITKSEASubSysSEIFConnectedSubSys;
    class FITKSEAJunctionPoint;
    class FITKSEAJunctionLine;
    class FITKSEAJunctionArea;
}

namespace VAOne
{
    class FITKVAOneXMLIOInterface;
    /**
     * @brief VAOne XML写出
     * @author LiBaojun (libaojunqd@foxmail.com)
     * @date 2024-10-11
     */
    class FITKVAOneXMLWriter : public Interface::FITKAbstractRapidXMLWriter
    {
    public:
        /**
         * @brief   适配器写出操作模板
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-16
         */
        template <typename T>
        bool adaptorWrite(QString nodeName, T* adaptor, rapidxml::xml_node<char>* node)
        {
            if (nodeName.isEmpty() || node == nullptr) return false;  // 检查node指针和节点名称是否为空

            this->consoleMessage(1, QString("Start write %1.").arg(nodeName));  // 开始写出节点数据

            adaptor = FITKIOADAPTORFACTORY->createT<T>("VAOneXML", nodeName);  // 获取写出适配器
            if (!adaptor) return false;
            adaptor->setNode(node);  // 设置节点

            bool ok = adaptor->adaptW();  // 适配器写出操作
            if (!ok) this->consoleMessage(3, QString("Write %1 Error.").arg(nodeName));

            delete adaptor;
            return ok;
        }
#if 1
        /**
         * @brief Construct a new FITKVAOneXMLWriter object
         * @param[i]  interface      组件指针，可传递参数
         * @param[o]  isSuccess      是否执行成功
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-10-11
         */
        explicit FITKVAOneXMLWriter(FITKVAOneXMLIOInterface* interface, bool & isScuccess);

        /**
         * @brief Destroy the FITKVAOneXMLWriter object
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-10-11
         */
        virtual ~FITKVAOneXMLWriter() = default;

        /**
         * @brief 打印控制台消息
         * @param[i] level 打印级别 1 normal 2 warning 3error
         * @param[i] str 打印的信息
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-03-08
         */
        void consoleMessage(int level, const QString& str) override;
#endif // 构建

        /**
         * @brief 执行写出
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-10-11
         */
        void run() override;

    private:
        /**
         * @brief writeNodes  写入Model数据
         * @param node[i]      xml节点
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2025-01-03
         */
        bool writeModel(rapidxml::xml_node<char> *node);
        
        /**
         * @brief writeNodes  写入Nodes数据
         * @param node[i]      xml节点
         * @author  lixiaolong (lixiaolong@diso.cn)
         * @date    2024-11-8
         */
        bool writeNodes(rapidxml::xml_node<char> *node);
#if 1
        /**
         * @brief writeFaces  写入面数据
         * @param node[i]      xml节点
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2024-12-24
         */
        bool writeFaces(rapidxml::xml_node<char> *node);

        /**
         * @brief writeFaces  写入板面数据
         * @param node[i]      xml节点
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2024-12-24
         */
        bool writeFacePlate(Acoustics::FITKSEAFaceManager *manager, rapidxml::xml_node<char>* node);

        /**
         * @brief writeFaces  写入单曲率面数据
         * @param node[i]      xml节点
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2024-12-24
         */
        bool writeFaceSingleCurve(Acoustics::FITKSEAFaceManager *manager, rapidxml::xml_node<char>* node);

        /**
         * @brief writeFaces  写入双曲率面数据
         * @param node[i]      xml节点
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2024-12-24
         */
        bool writeFaceDoubleCurve(Acoustics::FITKSEAFaceManager *manager, rapidxml::xml_node<char>* node);
#endif // 写入面数据

#if 1
        /**
         * @brief writeSubSystem  写入SEA子系统数据
         * @param node[i]      xml节点
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2024-12-24
         */
        bool writeSubSystem(rapidxml::xml_node<char> *node);

        /**
         * @brief   写入梁子系统数据
         * @param[i] dataManager 子系统管理器
         * @param[o] subSysNode xml节点
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2024-12-24
         */
        bool writeSubSystemBeam(Acoustics::FITKAcousticsSEASubSysManager *manager, rapidxml::xml_node<char>* node);

        /**
         * @brief   写入平板子系统数据
         * @param[i] dataManager 子系统管理器
         * @param[o] subSysNode xml节点
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2024-12-24
         */
        bool writeSubSystemFlatPlate(Acoustics::FITKAcousticsSEASubSysManager *manager, rapidxml::xml_node<char>* node);

        /**
         * @brief   写入单曲率板子系统数据
         * @param[i] dataManager 子系统管理器
         * @param[o] subSysNode xml节点
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2024-12-24
         */
        bool writeSubSystemSinglyCurvedShell(Acoustics::FITKAcousticsSEASubSysManager *manager, rapidxml::xml_node<char>* node);

        /**
         * @brief   写入双曲率板子系统数据
         * @param[i] dataManager 子系统管理器
         * @param[o] subSysNode xml节点
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2024-12-24
         */
        bool writeSubSystemDoublyCurvedShell(Acoustics::FITKAcousticsSEASubSysManager *manager, rapidxml::xml_node<char>* node);

        /**
         * @brief   写入圆柱面子系统数据
         * @param[i] dataManager 子系统管理器
         * @param[o] subSysNode xml节点
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2024-12-24
         */
        bool writeSubSystemCylinderShell(Acoustics::FITKAcousticsSEASubSysManager *manager, rapidxml::xml_node<char>* node);

        /**
         * @brief   写入声腔子系统数据
         * @param[i] dataManager 子系统管理器
         * @param[o] subSysNode xml节点
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2024-12-24
         */
        bool writeSubSystemCavity(Acoustics::FITKAcousticsSEASubSysManager *manager, rapidxml::xml_node<char>* node);

        /**
         * @brief   写入半无限流体子系统数据
         * @param[i] dataManager 子系统管理器
         * @param[o] subSysNode xml节点
         * @return  是否执行成功
         * @author  lilongyuan (lilongyuan@diso.cn)
         * @date    2024-12-24
         */
        bool writeSubSystemSemiIInfiniteFluid(Acoustics::FITKAcousticsSEASubSysManager *manager, rapidxml::xml_node<char>* node);
#endif // 写入SEA子系统数据

        /**
         * @brief writeMaterial  写入材料数据
         * @param node[i]      xml节点
         * @author  XingXuanCi (xingxuanci@diso.cn)
         * @date    2024-11-13
         */
        bool writeMaterial(rapidxml::xml_node<char> *node);

        /**
         * @brief writeMaterialFE  写入FE材料数据
         * @param node[i]      xml节点
         * @author  XingXuanCi (xingxuanci@diso.cn)
         * @date    2024-11-15
         */
        bool writeMaterialFE(rapidxml::xml_node<char> *node);

        /**
         * @brief writeSpectra  写入频谱数据
         * @param node[i]      xml节点
         * @author  XingXuanCi (xingxuanci@diso.cn)
         * @date    2024-11-13
         */
        bool writeSpectra(rapidxml::xml_node<char> *node);

        /**
         * @brief writeFrequencyDomain  写入频率数据
         * @param node[i]      xml节点
         * @author  XingXuanCi (xingxuanci@diso.cn)
         * @date    2024-11-13
         */
        bool writeFrequencyDomain(rapidxml::xml_node<char> *node);

        /**
         * @brief writeFrequencyDomain  写入物理属性节点数据
         * @param node[i]      xml节点
         * @author  liuzhiyuan(liuzhiyuan@diso.cn)
         * @date    2024-11-13
         */
        bool writePhysicalProp(rapidxml::xml_node<char> *node);

        /**
         * @brief writeFrequencyDomain  写入声学包节点数据
         * @param node[i]      xml节点
         * @author  liuzhiyuan(liuzhiyuan@diso.cn)
         * @date    2024-11-13
         */
        bool writeAdaptorNCT(rapidxml::xml_node<char> *node);

        /**
         * @brief 写入FE数据
         * @param node Model节点
         * @return  是否执行成功
         * @author lilongyuan (lilongyuan@diso.cn)
         * @date 2025-01-16
         */
        bool writeFEData(rapidxml::xml_node<char>* node);

    private:
        /**
        * @brief 将 QString 转换为 char*-（该函数后期会修改）
        * @param[in] qStr 要转换的 QString 对象
        * @return char* 返回指向动态分配的 char* 字符串
        * @throws std::runtime_error 如果字符串复制失败
        * @author lilongyuan (lilongyuan@diso.cn)
        * @date 2025-01-03
        */
        char* copyQStringToChar(const QString& qStr);
        
        /**
        * @brief   写入属性值
        * @param   node        对应节点
        * @param   attrName    属性名称
        * @param   value       字符串数值
        * @return  写入状态
        * @author  lilongyuan (lilongyuan@diso.cn)
        * @date    2025-01-03
        */
        bool writeAttribute(rapidxml::xml_node<char>* node, QString attrName, QString value);

        /**
        * @brief   创建节点子元素
        * @param   parentNode  父节点
        * @param   childName   子元素名称
        * @param   value       字符串类型的元素数据
        * @return  子元素节点
        * @author  lilongyuan (lilongyuan@diso.cn)
        * @date    2025-01-03
        */
        rapidxml::xml_node<char>* createNodeElement(rapidxml::xml_node<char>* parentNode, QString childName, QString value = "");

    private:
        /**
         * @brief 组件对象指针
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-10-11
         */
        FITKVAOneXMLIOInterface* _compInterface{};

        /**
         * @brief 是否执行成功
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-10-11
         */
        bool & _isSuccess;

        /**
         * @brief 声学离散模型，读取的信息存放在这里
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-10-11
         */
        Acoustics::FITKAcousticsDiscModel* _discModel{};

        /**
         * @brief 声学物理模型，读取的信息存放在这里
         * @author LiBaojun (libaojunqd@foxmail.com)
         * @date 2024-10-11
         */
        Acoustics::FITKAcousticsPhysics* _physics{};
    };
}



#endif
